import * as React from "react";
import PropTypes from "prop-types";
import {Classes, Dialog } from "@blueprintjs/core";
import InputPanel from '../../../../../component/InputPanel';
import axios from '../../../../../config/axios';
import {host} from '../../../../../config/host';
import {AppToaster} from "../../../../../component/Toast";

import './index.less';

class Edit extends React.PureComponent {
    constructor(props) {
        super(props);
        this.handleEditOk = this.handleEditOk.bind(this);
    }

    componentDidMount() {
    }

    async handleEditOk(inputValue) {
        const {onClose, menuType} = this.props;
        try {
            await axios.post(host + menuType.toLowerCase() + "/delete", inputValue);
            console.log(menuType.toLowerCase())
            await axios.post(host + menuType.toLowerCase() + "/create", inputValue);
            AppToaster.show({ message: "编辑成功" });
            this.props.onOk(menuType);
        } catch (e) {
            console.log(e)
            AppToaster.show({ message: "发生错误" });
        }
        onClose();
    }

    render() {
        const {editAttributes, editValue, method, onClose, menuType} = this.props;
        return(
            <Dialog
                icon="info-sign"
                onClose={onClose}
                title="编辑"
                {...this.state}
                isOpen={true}
            >
                <div className={Classes.DIALOG_BODY}>
                    <InputPanel attributes={editAttributes} values={editValue} method={method} menuType={menuType} handleOk={this.handleEditOk}/>
                </div>
            </Dialog>
        )
    }
}

Edit.propTypes = {
    editId: PropTypes.string.isRequired,
    editAttributes: PropTypes.array.isRequired,
    editValue:PropTypes.object.isRequired,
    method:PropTypes.string.isRequired,
    menuType: PropTypes.string.isRequired,
    onClose: PropTypes.func.isRequired,
    onOk:PropTypes.func.isRequired,
}

export default Edit;